﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKArchUtils.Utils.SelectMaterial
{
	public class FillPatternDrawingKit
	{
		public static Image CreateFillPatternImage(FillPattern fillPattern, double actualWidth, double actualHeight)
		{
			FillPatternDrawingKit.m_Path = Path.GetTempPath() + "tempImage\\";
			if (!Directory.Exists(FillPatternDrawingKit.m_Path))
			{
				Directory.CreateDirectory(FillPatternDrawingKit.m_Path);
			}
			string text = "";
			if (fillPattern != null)
			{
				text = FillPatternDrawingKit.m_Path + fillPattern.Name;
				if (File.Exists(text))
				{
					return new Bitmap(text, false);
				}
			}
			double num = (((actualWidth == 0.0) ? FillPatternDrawingKit.Width : actualWidth) == 0.0) ? 100.0 : ((actualWidth == 0.0) ? FillPatternDrawingKit.Width : actualWidth);
			if (double.IsNaN(num))
			{
				num = 100.0;
			}
			double num2 = (((actualHeight == 0.0) ? FillPatternDrawingKit.Height : actualHeight) == 0.0) ? 30.0 : ((actualHeight == 0.0) ? FillPatternDrawingKit.Height : actualHeight);
			if (double.IsNaN(num2))
			{
				num2 = 30.0;
			}
			Bitmap bitmap = new Bitmap((int)num, (int)num2);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				FillPatternDrawingKit.DrawFillPattern(graphics, fillPattern, actualWidth, actualHeight);
			}
			if (fillPattern != null)
			{
				try
				{
					bitmap.Save(text, ImageFormat.Bmp);
				}
				catch (Exception)
				{
					return bitmap;
				}
				return bitmap;
			}
			return bitmap;
		}

		private static void DrawFillPattern(Graphics graphics, FillPattern fillPattern, double actualWidth, double actualHeight)
		{
			float num = 50f;
			try
			{
				double num2 = (((actualWidth == 0.0) ? FillPatternDrawingKit.Width : actualWidth) == 0.0) ? 100.0 : ((actualWidth == 0.0) ? FillPatternDrawingKit.Width : actualWidth);
				if (double.IsNaN(num2))
				{
					num2 = 100.0;
				}
				double num3 = (((actualHeight == 0.0) ? FillPatternDrawingKit.Height : actualHeight) == 0.0) ? 30.0 : ((actualHeight == 0.0) ? FillPatternDrawingKit.Height : actualHeight);
				if (double.IsNaN(num3))
				{
					num3 = 30.0;
				}
				System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, (int)num2, (int)num3);
				float dx = (float)((rectangle.Left + rectangle.Left + rectangle.Width) / 2);
				float dy = (float)((rectangle.Top + rectangle.Top + rectangle.Height) / 2);
				graphics.TranslateTransform(dx, dy);
				System.Drawing.Rectangle rect = new System.Drawing.Rectangle(-(int)(num2 / 2.0), -(int)(num2 / 2.0), (int)num2, (int)num2);
				graphics.FillRectangle(Brushes.White, rect);
				if (fillPattern != null)
				{
					graphics.ResetTransform();
					IEnumerable<FillGrid> fillGrids = fillPattern.GetFillGrids();
					float num4;
                    if ((int)fillPattern.Target == 1)
					{
						num4 = num;
					}
					else
					{
						num4 = num * 20f;
					}
					foreach (FillGrid fillGrid in fillGrids)
					{
						float angle = (float)(fillGrid.Angle * 180.0 / Math.PI);
						Pen pen = new Pen(System.Drawing.Color.Black)
						{
							Width = 1f / num4
						};
						float num5 = 1f;
						IList<double> segments = fillGrid.GetSegments();
						if (segments.Count > 0)
						{
							pen.DashPattern = segments.Select(new Func<double, float>(Convert.ToSingle)).ToArray<float>();
							num5 = pen.DashPattern.Sum();
						}
						graphics.ResetTransform();
						Matrix matrix = new Matrix();
						matrix.Rotate(angle);
						Matrix matrix2 = new Matrix(1f, 0f, 0f, -1f, dx, dy);
						matrix2.Scale(num4, num4);
						matrix2.Translate((float)fillGrid.Origin.U, (float)fillGrid.Origin.V);
						Matrix matrix3 = matrix2.Clone();
						matrix3.Multiply(matrix);
						matrix2.Multiply(matrix);
						bool flag = true;
						for (int i = 20; i > 0; i--)
						{
							if (!flag)
							{
								matrix2.Translate((float)fillGrid.Shift, (float)fillGrid.Offset);
								matrix3.Translate((float)fillGrid.Shift, -(float)fillGrid.Offset);
							}
							else
							{
								flag = false;
							}
							float offsetX = -10f * num5;
							matrix2.Translate(offsetX, 0f);
							matrix3.Translate(offsetX, 0f);
							graphics.Transform = matrix2;
							graphics.DrawLine(pen, new PointF(0f, 0f), new PointF(200f, 0f));
							graphics.Transform = matrix3;
							graphics.DrawLine(pen, new PointF(0f, 0f), new PointF(200f, 0f));
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private static double Width = 300.0;

		private static double Height = 300.0;

		private static string m_Path = "";
	}
}
